package tuiLayer;
import controlLayer.PersonCtr;
import modelLayer.Person;
import java.util.Scanner;

/**
 * Write a description of class FriendsMenuUI here.
 * 
 * @author (Tobias) 
 * @version (0.1)
 */
public class FriendsMenuUI
{

    private PersonCtr ctrl;
    private Scanner scanner;
    /**
     * Constructor for objects of class FriendsMenuUI
     */
    public FriendsMenuUI()
    {
        ctrl = new PersonCtr();
        scanner = new Scanner(System.in);
    }
    
    public void start()
    {
        System.out.println("This is the friends menu:");
        System.out.println("1. Add friend");
        System.out.println("2. Update friend");
        System.out.println("3. Remove friend");
        System.out.println("4. Back");
        processInput();
    }
    
    private void processInput()
    {
        
        int input = Integer.parseInt(scanner.nextLine());
       switch(input)
       {
           case 1:
                addFriend();
           break;
           case 2:
                updateFriend();
           break;
           case 3:
                deleteFriend();
           break;
           case 4:
           break;
           default:
            System.out.println("invalid input");
            processInput();
           break;
        }
    }
    
    private void addFriend()
    {
        String name, address, zip, city, phone;
        
        name = getValue("name");
        address = getValue("address");
        zip = getValue("zipcode");
        city = getValue("city");
        phone = getValue("phone");
        
        ctrl.createPerson(name, address, zip, city, phone);
        start();
    }
    
    private void deleteFriend()
    {
        String phoneNumber = getValue("phone number");
        Person p = ctrl.findPerson(phoneNumber);
        ctrl.deletePerson(p);
        start();
    }
    
    private void updateFriend()
    {
        String name, address, zip, city, phone;
        String phoneNumber = getValue("phonenumber");
        Person p = ctrl.findPerson(phoneNumber);
        //TODO: fejlhåndtering
        System.out.println("Update info for " + p.getName());
        
        if(shouldUpdate("name"))
            name = getValue("name");
        else
            name = p.getName();
            
        if(shouldUpdate("address"))
            address = getValue("address");
        else
            address = p.getAddress();
            
        if(shouldUpdate("zip"))
            zip = getValue("zip");
        else
            zip = p.getPostalCode();
            
        if(shouldUpdate("city"))
            city = getValue("city");
        else
            city = p.getCity();
            
        if(shouldUpdate("phone"))
            phone = getValue("phone");
        else
            phone = p.getPhone();
            
        ctrl.updatePerson(p, name, address, zip, city, phone);
        System.out.println("success");
        start();
        
    }
    
    private boolean shouldUpdate(String name)
    {
        System.out.println("Update " + name + "? Y/N");
        
        String input = scanner.nextLine();
        
        switch(input.toLowerCase())
        {
            case "y":
            return true;
            case "n":
            return false;
            default:
                System.out.println("Invalid input");
                return shouldUpdate(name);
        }
    }
    
    private String getValue(String name)
    {
        System.out.println("Specify " + name + " :");
        return scanner.nextLine();
    }
}
